package com.example.kotlin

import org.jetbrains.kotlin.utils.keysToMap

fun main(args: Array<String>) {

    var builder = StringBuilder()
    builder.apply {
        append("hello world")
    }
    println(builder.toString())
    println("---------------------------------------分割线-------------------------------------")

    val myStringBuilder = StringBuilder().apply {
        append("Hello, ")
        append("World!")
    }
    println(myStringBuilder.toString()) // 输出: Hello, World!
    println("---------------------------------------分割线-------------------------------------")

    val number = 5.also { println("The number is: $it") } // 输出: The number is: 5
    println(number)
    println("---------------------------------------分割线-------------------------------------")

    val name: String? = "Kotlin"
    name?.let {
        println("Hello, $it!") // 输出: Hello, Kotlin!
    }
    println("---------------------------------------分割线-------------------------------------")

    var name2: String? = "Kotlin"
    name2 = null
    name2?.let {
        println("oh no, $it!")
    }
    println(name2)
    println("---------------------------------------分割线-------------------------------------")

    val result = run { // 代码块
        val x = 2
        val y = 3
        x + y // 返回结果
    }
    println(result) // 输出: 5
    println("---------------------------------------分割线-------------------------------------")

    val result1 = with(StringBuilder()) { // 与apply类似
        append("Kotlin ")
        append("is great!")
        toString() // 返回结果
    }
    println(result1) // 输出: Kotlin is great!
    println("---------------------------------------分割线-------------------------------------")

    val number1 = 10

    val result2 = number1.takeIf { it > 5 } ?: "Too small"
    println(result2) // 输出: 10

    val anotherResult = number1.takeUnless { it > 5 } ?: "Too small"
    println(anotherResult) // 输出: Too small
    println("---------------------------------------分割线-------------------------------------")

    val number2 = 10
    val result3 = number2.takeIf { it > 5 }
    val result4 = number2.takeIf { it > 100 }
    println(result3)
    println(result4)
    println("---------------------------------------分割线-------------------------------------")

    repeat(3) {
        println("Hello!") // 输出: Hello!（3次）
    }
    println("---------------------------------------分割线-------------------------------------")

    val lazyValue: String by lazy {
        println("Computed!")
        "Hello"
    }

    println(lazyValue) // 输出: Computed! Hello
    println(lazyValue) // 输出: Hello（不会再次计算）
    println("---------------------------------------分割线-------------------------------------")

    val list = listOf("apple", "banana", "cherry")
    val map = list.associate { it[0] to it.length } // 结果：{a=5, b=6, c=6}
    println(map)
    println("---------------------------------------分割线-------------------------------------")

    val fruits = listOf(Fruit("apple", "red"), Fruit("banana", "yellow"))
    val map1 =
        fruits.associateBy { it.name } // 结果：{apple=Fruit(name=apple, color=red), banana=Fruit(name=banana, color=yellow)}
    println(map1)
    println("---------------------------------------分割线-------------------------------------")

    val numbers = listOf(1, 2, 3, 4)
    val evenNumbers = numbers.filter { it % 2 == 0 } // 结果：[2, 4]
    println(evenNumbers)
    println("---------------------------------------分割线-------------------------------------")

    val squares = numbers.map { it * it } // 结果：[1, 4, 9]
    println(squares)
    println("---------------------------------------分割线-------------------------------------")

    val lists = listOf(listOf(1, 2), listOf(3, 4))
    val flattened = lists.flatMap { it } // 结果：[1, 2, 3, 4]
    println(flattened)
    println("---------------------------------------分割线-------------------------------------")

    val map2 = numbers.keysToMap { it * it } // 结果：{1=1, 2=4, 3=9}
    println(map2)
    println("---------------------------------------分割线-------------------------------------")

    val fruits1 = listOf("apple", "banana", "cherry", "avocado")
    val grouped = fruits1.groupBy { it.first() } // 结果：{a=[apple, avocado], b=[banana], c=[cherry]}
    println(grouped)

    println("---------------------------------------分割线-------------------------------------")
    numbers.forEach { println(it) } // 输出：1 2 3


}

data class Fruit(val name: String, val color: String)